import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { type UserInfo } from '../utils/interface'

export const useAuthStore = defineStore('auth', () => {
  const token = ref<any>()
  const userInfo = ref<any>()

  const isLoggedIn = computed(() => ref(token.value && token.value.length > 0))

  function getUserInfo() {
    return userInfo.value
  }

  function setUserInfo(newUserInfo: UserInfo) {
    userInfo.value = newUserInfo
  }

  function setLoginState(newToken: string, newUserInfo: UserInfo) {
    token.value = newToken
    userInfo.value = newUserInfo
  }

  function getToken() {
    return token.value
  }
  function setToken(newToken: string) {
    token.value = newToken
  }

  function clearLoginState() {
    token.value = ''
    userInfo.value = {}
  }

  return {
    token,
    userInfo,
    isLoggedIn,

    getToken,
    setToken,
    getUserInfo,
    setUserInfo,
    setLoginState,
    clearLoginState
  }
})
